package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.ResultObj;
import com.southwind.entity.Equipment;
import com.southwind.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-12-17
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @GetMapping("/list")
    public Map list(Integer page, Integer limit, String name){
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "equipment_name", name);
        Page<Equipment> pageModel = new Page<>(page, limit);
        Page<Equipment> resultPage = this.equipmentService.page(pageModel,queryWrapper);
        Map map = new HashMap();
        map.put("data", resultPage.getRecords());
        map.put("count", resultPage.getTotal());
        return map;
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody Equipment equipment){
        boolean save = this.equipmentService.save(equipment);
        if(save) return ResultObj.EQUIPMENT_ADD_SUCCESS;
        return ResultObj.EQUIPMENT_ADD_ERROR;
    }

    @PostMapping("/update")
    public ResultObj update(@RequestBody Equipment equipment){
        boolean updateById = this.equipmentService.updateById(equipment);
        if(updateById) return ResultObj.EQUIPMENT_UPDATE_SUCCESS;
        return ResultObj.EQUIPMENT_UPDATE_ERROR;
    }

    @DeleteMapping("/delete/{equipmentId}")
    public ResultObj delete(@PathVariable("equipmentId") Integer equipmentId){
        boolean removeById = this.equipmentService.removeById(equipmentId);
        if(removeById) return ResultObj.EQUIPMENT_DELETE_SUCCESS;
        return ResultObj.EQUIPMENT_DELETE_ERROR;
    }

}

